package com.lucky.ebidding.controller;

import com.lucky.ebidding.dto.ExpertDTO;
import com.lucky.ebidding.model.Expert;
import com.lucky.ebidding.model.User;
import com.lucky.ebidding.service.ExpertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ClassName:ExpertController
 * Package:com.lucky.ebidding.controller
 * Description:
 *
 * @Date:2020/5/13 16:46
 * @Author:857251389@qq.com
 */
@Controller
@RequestMapping("/expert")
public class ExpertController {

    @Autowired
    private ExpertService expertService;

    @GetMapping("/addJump")
    public String expertAddJump(HttpServletRequest request, Model model){
        User user = (User) request.getSession().getAttribute("user");

        Expert expert = expertService.selectByUserId(user.getId());

        model.addAttribute("expert",expert);
        return "expert/expert_message_add.html";
    }

    @PostMapping("/add")
    public String expertAdd(Expert expert,HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");

        expertService.insertAdd(expert);

        return "redirect:/";
    }


    @ResponseBody
    @PostMapping("/select")
    public ExpertDTO select(Integer number, Integer type, Integer zbId, HttpServletRequest request){
        if(number == null || type ==null){
            ExpertDTO expertDTO = new ExpertDTO();
            expertDTO.setCode(0);
            expertDTO.setMsg("无数据");
            return expertDTO;
        }
        User user = (User) request.getSession().getAttribute("user");

        List<Expert> experts = expertService.selectExpert(number, type, zbId, user);

        ExpertDTO expertDTO = new ExpertDTO();
        expertDTO.setCode(0);
        expertDTO.setMsg("yes");
        expertDTO.setCount(experts.size());
        expertDTO.setData(experts);
        return expertDTO;
    }
}
